/**
 * com.future CO.,ltd.
 */

package com.future.springcloud.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;

import java.util.Locale;
import java.util.concurrent.TimeUnit;

/**
 * 类说明：
 */
@Service
public class PaymentService {

	/**
	 * 正常场景
	 *
	 * @param id
	 * @return
	 */
	public String paymentInfoOK(Integer id){
		System.out.println("线程池："+Thread.currentThread().getName()+" OK, id:"+id);
		return "线程池："+Thread.currentThread().getName()+" OK, id:"+id;
	}

	/**
	 * 异常场景
	 *
	 * @param id
	 * @return
	 */
	@HystrixCommand(fallbackMethod = "timeoutHandler",commandProperties = {
			@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000")
	})
	public String paymentInfoTimeOut(Integer id){
		try {
			TimeUnit.SECONDS.sleep(3);
		}catch (Exception e){
			e.printStackTrace();
		}
		System.out.println("线程池："+Thread.currentThread().getName()+" timeOut, id:"+id);
		return "线程池："+Thread.currentThread().getName()+" timeOut, id:"+id;
	}

	private String timeoutHandler(Integer id){
		String errMsg = String.format(Locale.ENGLISH,"系统繁忙或者运行报错，请稍后再试，id：%s",id);
		System.out.println(errMsg);
		return errMsg;
	}
}
